
// 监听来自内容脚本的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === "autoOpenProductViewer") {
    chrome.storage.sync.get('floatingWindowOn', (data) => {
      if (data.floatingWindowOn === undefined) {
        chrome.storage.sync.set({ floatingWindowOn: true }, () => {
          sendResponse({ floatingWindowOn: true });
        });
      } else {
        sendResponse(data);
      }
    });
    return true; // 保持消息通道开放，以便异步发送响应
  }
});

// 监听标签更新事件
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
  if (changeInfo.status === 'complete' && tab.url.includes('jd.com')) {
    chrome.tabs.sendMessage(tabId, { action: "pageLoaded" });
  }
});

// 安装或更新扩展时的处理
chrome.runtime.onInstalled.addListener((details) => {
  if (details.reason === "install") {
    // 首次安装时的处理
    chrome.storage.sync.set({
      settings: {
        productsPerPage: 20,
        autoLoad: true
      }
    });
  } else if (details.reason === "update") {
    // 更新时的处理
    console.log("Extension updated");
  }
});